var helper = {
    TimestampToTime : function (timestamp, format ) {
        if (timestamp < 9999999999) {
            timestamp *= 1000;
        }
        format = format || "Y-M-D h:m:s";
        var date = new Date(timestamp);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
        var Y = date.getFullYear() ;
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) ;
        var D = date.getDate();
        var h = date.getHours();
        var m = date.getMinutes();
        var s = date.getSeconds();
        return format.replace( /Y/, Y ).replace( /M/, M ).replace( /D/, D ).replace( /h/, h ).replace( /m/, m ).replace( /s/, s );
    },
    isWebGl: function () {
        if (cc._renderType === cc.game.RENDER_TYPE_CANVAS) {
            return false;
        }
        return true;
    },
    isLeapYear: function (Year) {
        if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
            return (true);
        } else { return (false); }
    },
    getBase64Image: function (img) {
        var canvas = document.createElement("canvas");
        canvas.width = img.width;
        canvas.height = img.height;
        var ctx = canvas.getContext("2d");
        ctx.drawImage(img, 0, 0, img.width, img.height);
        var dataURL = canvas.toDataURL();
        return dataURL;
    },
    loadImg: function (base64, sprite) {
        var img = document.createElement("img");
        img.src = base64;
        img.onload = function () {
            var texture = new cc.Texture2D();
            texture.initWithElement(img);
            texture.handleLoadedTexture();
            var frame = new cc.SpriteFrame(texture);
            sprite.spriteFrame = frame;
        };
    },
    checkMobile: function (str) {
        if (!(/^1[3|4|5|6|7|8][0-9]\d{4,8}$/.test(str))) {
            return false;
        }
        return true;
    },
    fixPhotoSize: function (node, now) {
        if (now) {
            if (cc.isValid(node)) {
                var oldWidth = node.width;
                var oldHeight = node.height;

                node.height = node.parent.height;
                var ph = node.height / oldHeight;
                node.width = oldWidth * ph;

                oldWidth = node.width;
                oldHeight = node.height;
                if (node.width < node.parent.width) {
                    node.width = node.parent.width;
                    var ph = node.width / oldWidth;
                    node.height = oldHeight * ph;
                }
            }
        }
        else {
            node.once("size-changed", function () {
                if (cc.isValid(node)) {
                    var oldWidth = node.width;
                    var oldHeight = node.height;

                    node.height = node.parent.height;
                    var ph = node.height / oldHeight;
                    node.width = oldWidth * ph;

                    oldWidth = node.width;
                    oldHeight = node.height;
                    if (node.width < node.parent.width) {
                        node.width = node.parent.width;
                        var ph = node.width / oldWidth;
                        node.height = oldHeight * ph;
                    }
                }
            }, this);
        }
    },
    shot: function( targetNode, cullingMask ) {
        let node = new cc.Node();
        node.parent = targetNode;
        node.setPosition(0, 0);
        let camera = node.addComponent(cc.Camera);
        if( cullingMask ) {
            camera.cullingMask = cullingMask;
        }

        // 新建一个 RenderTexture，并且设置 camera 的 targetTexture 为新建的 RenderTexture，这样 camera 的内容将会渲染到新建的 RenderTexture 中。
        let texture = new cc.RenderTexture();
        let gl = cc.game._renderContext;
        // 如果截图内容中不包含 Mask 组件，可以不用传递第三个参数
        texture.initWithSize(targetNode.width, targetNode.height, gl.STENCIL_INDEX8);
        camera.targetTexture = texture;

        // 渲染一次摄像机，即更新一次内容到 RenderTexture 中
        camera.render();
        
        return texture;
    },
    shotToBase64: function( targetNode, cullingMask ) {
        let texture = this.shot( targetNode, cullingMask );

        // 这样我们就能从 RenderTexture 中获取到数据了
        let data = texture.readPixels();
        // 接下来就可以对这些数据进行操作了
        let canvas = document.createElement('canvas');
        let ctx = canvas.getContext('2d');
        canvas.width = texture.width;
        canvas.height = texture.height;

        let width = texture.width;
        let height = texture.height;
        let rowBytes = width * 4;
        for (let row = 0; row < height; row++)
        {
            let srow = height - 1 - row;
            let imageData = ctx.createImageData(width, 1);
            let start = srow * width * 4;
            for (let i = 0; i < rowBytes; i++) {
                imageData.data[i] = data[start + i];
            }

            ctx.putImageData(imageData, 0, row);
        }
        return canvas.toDataURL();
    },


    base64: {
        // private property
        _keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

        // public method for encoding
        encode: function (input) {
            var output = "";
            var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
            var i = 0;
            input = helper.base64._utf8_encode(input);
            while (i < input.length) {
                chr1 = input.charCodeAt(i++);
                chr2 = input.charCodeAt(i++);
                chr3 = input.charCodeAt(i++);
                enc1 = chr1 >> 2;
                enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
                enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
                enc4 = chr3 & 63;
                if (isNaN(chr2)) {
                    enc3 = enc4 = 64;
                } else if (isNaN(chr3)) {
                    enc4 = 64;
                }
                output = output +
                    helper.base64._keyStr.charAt(enc1) + helper.base64._keyStr.charAt(enc2) +
                    helper.base64._keyStr.charAt(enc3) + helper.base64._keyStr.charAt(enc4);
            }
            return output;
        },

        // public method for decoding
        decode: function (input) {
            var output = "";
            var chr1, chr2, chr3;
            var enc1, enc2, enc3, enc4;
            var i = 0;
            input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
            while (i < input.length) {
                enc1 = helper.base64._keyStr.indexOf(input.charAt(i++));
                enc2 = helper.base64._keyStr.indexOf(input.charAt(i++));
                enc3 = helper.base64._keyStr.indexOf(input.charAt(i++));
                enc4 = helper.base64._keyStr.indexOf(input.charAt(i++));
                chr1 = (enc1 << 2) | (enc2 >> 4);
                chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
                chr3 = ((enc3 & 3) << 6) | enc4;
                output = output + String.fromCharCode(chr1);
                if (enc3 != 64) {
                    output = output + String.fromCharCode(chr2);
                }
                if (enc4 != 64) {
                    output = output + String.fromCharCode(chr3);
                }
            }
            output = helper.base64._utf8_decode(output);
            return output;
        },

        // private method for UTF-8 encoding
        _utf8_encode: function (string) {
            string = string.replace(/\r\n/g, "\n");
            var utftext = "";
            for (var n = 0; n < string.length; n++) {
                var c = string.charCodeAt(n);
                if (c < 128) {
                    utftext += String.fromCharCode(c);
                } else if ((c > 127) && (c < 2048)) {
                    utftext += String.fromCharCode((c >> 6) | 192);
                    utftext += String.fromCharCode((c & 63) | 128);
                } else {
                    utftext += String.fromCharCode((c >> 12) | 224);
                    utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                    utftext += String.fromCharCode((c & 63) | 128);
                }

            }
            return utftext;
        },

        // private method for UTF-8 decoding
        _utf8_decode: function (utftext) {
            var string = "";
            var i = 0;
            var c = c1 = c2 = 0;
            while (i < utftext.length) {
                c = utftext.charCodeAt(i);
                if (c < 128) {
                    string += String.fromCharCode(c);
                    i++;
                } else if ((c > 191) && (c < 224)) {
                    c2 = utftext.charCodeAt(i + 1);
                    string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                    i += 2;
                } else {
                    c2 = utftext.charCodeAt(i + 1);
                    c3 = utftext.charCodeAt(i + 2);
                    string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                    i += 3;
                }
            }
            return string;
        }
    }
};

require("utils").helper = helper;
module.exports = helper;